<?php
/*
    buttons are: next, prev, proofer next, proofer prev
    which needs current projectid, pagename, roundid, proofer
    to make query SELECT pagename FROM pages
                  WHERE projectid ... AND pagename < or > pagename
                  (AND maybe roundn_user = joe)
*/

$relPath="./../../pinc/";
require_once $relPath."dpinit.php";
require_once $relPath."dp_main.inc";
require_once $relPath.'slim_header.inc';
require_once $relPath.'rounds.php';
require_once "DifferenceEngineWrapper.inc";

$User->IsLoggedIn()
    or RedirectToLogin();

$projectid       = ArgProjectId();
$argpagename     = ArgPageName();
$roundid         = ArgRoundId();

if(! $projectid)
    die(_("Project ID not provided."));
if(! $argpagename)
    die(_("Page name not provided."));
if(! $roundid)
    die(_("Round ID not provided."));

$page            = new DpPage($projectid, $argpagename);
$proofername     = $page->RoundUser($roundid);
$prevroundid     = RoundIdBefore($roundid);
$prevproofername = $page->RoundUser($prevroundid);

$btnnext         = IsArg("btnnext");
$btnprev         = IsArg("btnprev");
$btnprfnext      = IsArg("btnprfnext");
$btnprfprev      = IsArg("btnprfprev");

// get the project
$project         = new DpProject($projectid);


// determine what page is wanted
if($btnnext) {
    $pgname = $project->PageNameAfter($argpagename);
    if($pgname) {
        $pagename = $pgname;
    }
}
else if($btnprev) {
    $pgname = $project->PageNameBefore($argpagename);
    if($pgname) {
        $pagename = $pgname;
    }
}
else if($btnprfnext) {
    $pgname = $project->ProoferRoundPageNameAfter($argpagename,
                                                    $roundid);
    if($pgname) {
        $pagename = $pgname;
    }
}
else if($btnprfprev) {
    $pgname = $project->ProoferRoundPageNameBefore($argpagename,
                                                    $roundid);
    if($pgname) {
        $pagename = $pgname;
    }
}

if(empty($pagename)) {
    $pagename = $argpagename;
}

// get the page
$page        = new DpPage($projectid, $pagename);

$proofername     = $page->RoundUser($roundid);
$prevproofername = $page->RoundUser($prevroundid);
$imagefile       = $page->ImageFileName();
$project_title   = $page->Title();
$text            = $page->RoundText($roundid);
$prevtext        = $page->RoundText($prevroundid);

$label           = $roundid 
                 . $project->UserMaySeeNames()
                     ? $page->RoundUser($roundid)
                     : "";
$prevlabel       = $prevroundid
                 . $project->UserMaySeeNames()
                     ? $page->RoundUser($prevroundid)
                     : "";

// now have the image, users, labels etc all set up
// -----------------------------------------------------------------

$title      = "Diff pg. {$pagename} {$page->Title()}";
$imgurl     = $page->ImageUrl();
$projlink   = link_to_project($projectid, "Go to project page");

$diffEngine = new DifferenceEngineWrapper();
$view_image = _("view image");

echo "<!DOCTYPE HTML>
<html " . lang_html_header() . ">
<head>
<meta charset=utf-8>
<title>$title</title>
<link type='text/css' rel='stylesheet' href='{$css_url}/dp.css'>
<script type='text/javascript' src='dpdiff.js'></script>
</head>

<body onload='init()'>
<div id='container center'>

<h1>$title</h1>

<div id='diffbox' class='center w80'>
    <span class='likealink'onclick='eShowImage()'>
            {$view_image}
    </span>
    <form id='navform' name='navform' method='GET' 
            accept-charset='UTF-8' class='right' target=''>\n";

    echo "
        <div class='lfloat'>
        <input type='submit' name='btnprev' value='"
            ._("Previous")."'>

        ". _("Jump to:") . " 
        <select id='pagelist' onChange='eListChange()'>\n";

    foreach($project->PageRows() as $row)
    {
        $sel = ($row['fileid'] === $pagename
            ? " selected='selected' "
            : "");
        $name = $row['fileid'];
        echo "<option value='$name' {$sel}>$name</option>\n";
    } 

    echo "
        </select>\n";

    echo "
        <input type='submit' name='btnnext' value='"._("Next")."'>\n";


echo "
        </div>
        <div class='rfloat'>
        <input type='submit' name='btnprfprev' 
                            value='"._("Proofreader Prev")."'>
        <input type='submit' name='btnprfnext' 
                                value='"._("Proofreader Next")."'>
        </div>

        <input type='hidden' 
            id='projectid' name='projectid' value='$projectid'>
        <input type='hidden' 
            id='pagename' name='pagename' value='$pagename'>
        <input type='hidden' id='roundid' name='roundid' 
            value='$roundid'>
        <input type='hidden' id='imagepath' value='{$imgurl}'>
    </form>
    <p>{$projlink}</p>
    </div>\n";

    if($prevtext == $text)
    {
        echo "
        <div class='w50 center'>
            <h1>"._("No differences.")."</h1>
        </div>\n";
    }
    else
    {
        $diffEngine->showDiff($prevtext, $text, $prevlabel, $label);
    }
echo "
</body></html>\n";

// ---------------------------------------------------------


// theme_footer();
exit;


// vim: sw=4 ts=4 expandtab
?>
